﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace NoDriverPrintHelperV1d0.Entity
{
    /// <summary>
    /// 打印文本信息
    /// </summary>
    [XmlRoot("内容项")]
    public class PrintTextInfoV1d0
    {
        [XmlAttribute("字体颜色")]
        public string FontColorName { get; set;}
        [XmlAttribute("文本内容")]
        public string Text { get; set; }
        /// <summary>
        /// 文字尺寸 标注，倍宽，倍高，
        /// </summary>
        [XmlAttribute("是否倍高")]
        public bool IsDoubleHeight { get; set; }
        [XmlAttribute("是否倍宽")]
        public bool IsDoubleWidth { get; set; }
        [XmlAttribute("是否粗体")]
        public bool IsBold { get; set; }
        /// <summary>
        /// 文字编码名称 Big5 GB2312 GB18030
        /// </summary>
        [XmlAttribute("文字编码")]
        public string EncodingName { get; set; }
        /// <summary>
        /// 文字位图的像素宽度 
        /// </summary>
        [XmlAttribute("文字位图宽度")]
        public int TextImageWidth { get; set; }
        /// <summary>
        /// 文字位图的像素高度
        /// </summary>
        [XmlAttribute("文字位图高度")]
        public int TextImageHeight { get; set; }

        /// <summary>
        /// 打印模式 text image 字符打印或者图像打印
        /// </summary>
        [XmlAttribute("打印模式")]
        public string PrintMode { get; set; }

        [XmlAttribute("位图打印模式左边距")]
        public int ImageLeftSpace { get; set; }
        [XmlAttribute("位图打印模式右边距")]
        public int ImageRightSpace { get; set; }
        [XmlAttribute("位图打印模式字体")]
        public string ImageFontFamilyName { get; set; }
        [XmlAttribute("位图打印模式字号")]
        public int ImageFontSize { get; set; }

        [XmlAttribute("字符打印模式对齐方式")]
        public string Align { get; set; }

        public PrintTextInfoV1d0()
        {
            FontColorName = "Black";// "Red"
            Text = "";
            IsDoubleWidth = false;
            IsDoubleHeight = false;
            IsBold = false;
            EncodingName = "Big5";
            TextImageWidth = 400;
            TextImageHeight = 24;
            ImageLeftSpace = 0;
            ImageRightSpace = 0;
            PrintMode = "Text";// "Image"
            Align = "Left";//"Middle"  "Right"
            ImageFontFamilyName = "宋体";
            ImageFontSize = 8;
        }

        /// <summary>
        /// 根据文本内容，左边距，字体，图像宽度，得到位图(位图高度是自动计算的)
        /// </summary>
        /// <param name="textInfo">待打印文本信息</param>
        /// <returns>待打印位图</returns>
        public static Bitmap GetAutoHeightBitmapFromText(PrintTextInfoV1d0 textInfo)
        {
            Font printFont = new Font(textInfo.ImageFontFamilyName, textInfo.ImageFontSize, FontStyle.Regular);
            return GetAutoHeightBitmapFromText(
                textInfo.Text, textInfo.ImageLeftSpace, textInfo.ImageRightSpace, printFont, textInfo.TextImageWidth);
        }

        public static Bitmap GetAutoHeightBitmapFromText(PrintTaskItemV1d0 taskItem)
        {
            Font printFont = new Font(taskItem.TextFormat.image_font_family_name, taskItem.TextFormat.image_font_size, FontStyle.Regular);
            return GetAutoHeightBitmapFromText(
                taskItem.content_text, taskItem.TextFormat.image_left_space, taskItem.TextFormat.image_right_space, printFont, taskItem.TextFormat.text_image_width);
        }
        /// <summary>
        /// 根据文本内容，左边距，字体，图像宽度，得到位图(位图高度是自动计算的)
        /// </summary>
        /// <param name="text">用图像模式进行打印的文本</param>
        /// <param name="leftSpace">左边距</param>
        /// <param name="printFont">打印字体</param>
        /// <param name="imageWidth">图像像素宽度</param>
        /// <returns>位图</returns>
        public static Bitmap GetAutoHeightBitmapFromText(string text, int leftSpace, int rightSpace,Font printFont, int imageWidth = 400)
        {
            //得到适应文本的图像的高度
            int imageHeight = GetImageHeightOfText(text, leftSpace, rightSpace, printFont, imageWidth);
            Bitmap textBitmap = new Bitmap(imageWidth, imageHeight);
            Graphics iG = Graphics.FromImage(textBitmap);
            Brush fontBrush = new SolidBrush(Color.Black);
            iG.Clear(Color.White);        //赋予图像一个背景色
            int usingImageWidth = imageWidth - leftSpace - rightSpace;
            //StringFormat sf = new StringFormat();
            //sf.Alignment = StringAlignment.Center;
            //sf.LineAlignment = StringAlignment.Center;
            iG.DrawString(text, printFont, fontBrush, new Rectangle(leftSpace, 0, usingImageWidth, imageHeight));
            iG.Dispose();
            return textBitmap;
        }
        /// <summary>
        /// 根据文本内容，左边距，字体，图像宽度，自动计算适应该文本的最小图像高度（8的整倍数）
        /// </summary>
        /// <param name="text">待绘制到画板上的文本</param>
        /// <param name="leftSpace">左边距</param>
        /// <param name="printFont">字体</param>
        /// <param name="imageWidth">图像宽度</param>
        /// <returns>可容纳该文本内容的最小图像高度（8的整数倍）</returns>
        private static int GetImageHeightOfText(string text, int leftSpace,int rightSpace, Font printFont, int imageWidth)
        {
            int resultHeight = 0;
            //左边距，右边距
            float usingImageWidth = imageWidth - leftSpace-rightSpace;
            Bitmap textBitmap = new Bitmap((int)usingImageWidth, 24);
            Graphics iG = Graphics.FromImage(textBitmap);
            //计算该字符串使用该字体时，不换行时宽度为多少像素，高度为多少像素
            SizeF textSizeF = iG.MeasureString(text, printFont);
            float textWidth = textSizeF.Width;
            float textHeight =textSizeF.Height;
            //当前宽度下，显示text需要多少行, 计算行数和高度是全部采取上取整
            int rowCount = (int)(Math.Ceiling(textWidth / usingImageWidth));
            int imageHeightNeeded = (int)Math.Ceiling((rowCount * textHeight));
            int printLineCount = imageHeightNeeded / 8+1;
            //行间距
            resultHeight = printLineCount * 8;
            iG.Dispose();
            textBitmap.Dispose();
            return resultHeight;
        }
    }



}
